package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.QxXtGndyMapper;
import com.tfswx.fljdmxyy.dto.gndy.*;
import com.tfswx.fljdmxyy.dto.rygl.RyxxOutputDTO;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.QxXtGndy;
import com.tfswx.fljdmxyy.query.gndy.GndyQueryParam;
import com.tfswx.fljdmxyy.query.gndy.GndyQueryResult;
import com.tfswx.fljdmxyy.service.GndyService;
import com.tfswx.fljdmxyy.service.RyglService;
import com.tfswx.fljdmxyy.service.WjfwService;
import com.tfswx.fljdmxyy.utils.ConverterUtils;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import com.tfswx.newbie.boot.starter.page.PageHelper;
import com.tfswx.newbie.boot.starter.page.PageParam;
import com.tfswx.newbie.boot.starter.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 功能定义业务实现类
 *
 * @author: 陈文佳
 * @date: 2022-10-13
 **/
@Slf4j
@Service
public class GndyServiceImpl implements GndyService {

    @Resource
    private WjfwService wjfwService;

    @Resource
    private QxXtGndyMapper qxXtGndyMapper;

    @Resource
    private RyglService ryglService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GndyAddOutputDTO addGndy(GndyAddInputDTO inputDTO) {
        // 1.获取用户信息
        RyxxOutputDTO ryxxOutputDTO = ryglService.getUserInfo();
        if (ObjectUtil.isNull(ryxxOutputDTO)) {
            throw new BusinessException(BusinessCodeEnum.HQSJSB);
        }

        // 2.设置功能定义参数
        QxXtGndy gndy = ConverterUtils.convert(inputDTO, QxXtGndy.class);
        gndy.setGnbm(UuidUtils.uuid32());
        Date now = new Date();
        gndy.setCjsj(now);
        gndy.setZhxgsj(now);
        gndy.setCjrbm(ryxxOutputDTO.getRybm());
        gndy.setCjrmc(ryxxOutputDTO.getRymc());
        gndy.setZhxgrbm(ryxxOutputDTO.getRybm());
        gndy.setZhxgrmc(ryxxOutputDTO.getRymc());

        // 3.设置功能序号
        if (ObjectUtil.isEmpty(gndy.getGnxh())) {
            Integer gndyMaxXh = qxXtGndyMapper.getGndyMaxXh(gndy);
            if (ObjectUtil.isNull(gndyMaxXh)) {
                gndyMaxXh = 0;
            }
            gndy.setGnxh(gndyMaxXh + 1);
        }

        // 4.保存功能定义
        int res = qxXtGndyMapper.insertGndy(gndy);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }
        String gntb = gndy.getGntb();
        if (StrUtil.isNotBlank(gntb)) {
            // 5.如果传入了图标 确认功能图标文件
            wjfwService.confirmFile(SystemConstant.ZYLXBM_GNDY, gndy.getGnbm(), gndy.getGntb());
        }
        return ConverterUtils.convert(gndy, GndyAddOutputDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGndy(GndyUpdateInputDTO inputDTO) {
        // 1.获取用户信息
        RyxxOutputDTO ryxxOutputDTO = ryglService.getUserInfo();
        if (ObjectUtil.isNull(ryxxOutputDTO)) {
            throw new BusinessException(BusinessCodeEnum.HQSJSB);
        }

        // 2.查询修改前的功能定义
        QxXtGndy gndyParam = ConverterUtils.convert(inputDTO, QxXtGndy.class);
        QxXtGndy oldGndy = qxXtGndyMapper.getGndy(gndyParam);
        if (ObjectUtil.isNull(oldGndy)) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }
        gndyParam.setZhxgsj(new Date());
        gndyParam.setZhxgrbm(ryxxOutputDTO.getRybm());
        gndyParam.setZhxgrmc(ryxxOutputDTO.getRymc());

        // 3.修改功能定义
        int res = qxXtGndyMapper.updateGndy(gndyParam);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }

        // 4.如果没有传入图标 则删除图标
        if (StrUtil.isBlank(gndyParam.getGntb())) {
            wjfwService.deleteFile(SystemConstant.ZYLXBM_GNDY, gndyParam.getGnbm());
        } else {
            // 5.如果传入了新的图标 则删除旧图标 确认新图标 反之 不做操作
            if (!StrUtil.equals(oldGndy.getGntb(), gndyParam.getGntb())) {
                wjfwService.deleteFile(SystemConstant.ZYLXBM_GNDY, gndyParam.getGnbm());
                wjfwService.confirmFile(SystemConstant.ZYLXBM_GNDY, gndyParam.getGnbm(), gndyParam.getGntb());
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public GndyGetOutputDTO getGndy(GndyGetInputDTO inputDTO) {
        QxXtGndy gndyParam = ConverterUtils.convert(inputDTO, QxXtGndy.class);
        return ConverterUtils.convert(qxXtGndyMapper.getGndy(gndyParam), GndyGetOutputDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGndy(GndyDeleteInputDTO inputDTO) {
        inputDTO.getGnbm().forEach(gnbm -> {
            // 1.查询功能定义
            QxXtGndy gndyParam = new QxXtGndy();
            gndyParam.setGnbm(gnbm);
            QxXtGndy gndy = qxXtGndyMapper.getGndy(gndyParam);
            if (ObjectUtil.isNull(gndy)) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }

            // 2.删除功能定义
            gndyParam.setZhxgsj(new Date());
            int res = qxXtGndyMapper.deleteGndy(gndyParam);
            if (res < 1) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }

            // 3.如果存在图标则删除图标
            if (StrUtil.isNotBlank(gndy.getGntb())) {
                wjfwService.deleteFile(SystemConstant.ZYLXBM_GNDY, gndyParam.getGnbm());
            }
        });
        return Boolean.TRUE;
    }

    @Override
    public PageResult<GndyQueryResult> queryGndy(PageParam<GndyQueryParam> param) {
        return PageHelper.doSelectPage(param, () -> qxXtGndyMapper.queryGndy(param.getFilter()));
    }
}